namespace Digitalmes.Application.Services.Impl;

/// <summary>
/// 物料条码校验服务。
/// </summary>
internal sealed class MaterialMatchService(ISysConfigQueryService sysConfigQueryService) : IMaterialMatchService
{
    public async Task<bool> MatchAsync(string barcode, params IEnumerable<string> rules)
    {
        var matchFunc = await GetMatchFuncAsync();
        return matchFunc(barcode, rules);
    }

    public async Task<Func<string, IEnumerable<string>, bool>> GetMatchFuncAsync()
    {
        var placeholder = await sysConfigQueryService.GetValueAsync(ConfigKeys.RuleBarcodePlaceholder);
        var c = !string.IsNullOrWhiteSpace(placeholder) ? placeholder[0] : '$';
        var notWholeMatched = await sysConfigQueryService.IsYes(ConfigKeys.AllowBarcodeRuleNotWholeMatched);

        return (string barcode, IEnumerable<string> rules) => StringUtils.Match(barcode, rules, c, notWholeMatched);
    }
}
